//
//  MapBomView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/23.
//

import UIKit

class MapBomView: UIView {
    
    lazy var scrollView: UIScrollView = {
        let scro = UIScrollView.init(frame: .zero)
        scro.backgroundColor = .clear
        scro.showsHorizontalScrollIndicator = true
        scro.showsVerticalScrollIndicator = false
        return scro
    }()
    
    var userViewArr = [MapBomUserView]()

    var userArr = [PositionModel]() {
        didSet {
            for item in scrollView.subviews {
                item.removeFromSuperview()
            }
            userViewArr.removeAll()
            
            let allW = CGFloat(userArr.count)*(scaleWidth(80)+48)-12
            if allW >= (ScreenW-24) {
                scrollView.snp.updateConstraints { make in
                    make.width.equalTo(ScreenW-24)
                }
            }else {
                scrollView.snp.updateConstraints { make in
                    make.width.equalTo(allW)
                }
            }
            for i in 0..<userArr.count {
                let view = MapBomUserView()
                view.frame = CGRect(x: CGFloat(i)*(scaleWidth(80)+48), y: 0, width: (scaleWidth(80)+36), height: scaleWidth(116)+103)
                view.model = userArr[i]
                userViewArr.append(view)
                scrollView.addSubview(view)
            }
            scrollView.contentSize = CGSize(width: allW, height: 0)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.bottom.equalTo(0)
            make.width.equalTo(0)
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class MapBomUserView: UIView {

    lazy var userImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_header"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(18)
        return image
    }()

    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(4)
        image.clipsToBounds = true
        return image
    }()

    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .black
        label.numberOfLines = 2
        return label
    }()
    
    lazy var bookBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.rx.tap.bind { [weak self] in
            self?.viewContainingController?.wn_pushBookVC(self?.model?.bookId ?? "")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    var model: PositionModel? {
        didSet {
            if let m = model {
                userImage.wn_setupImg(m.avatar, placeHolder: "placeholder_header")
                bookImage.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(userImage)
        addSubview(bookImage)
        addSubview(nameLabel)
        addSubview(bookBtn)
        userImage.snp.makeConstraints { (make) in
            make.top.equalTo(12)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(36)
        }
        bookImage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(userImage.snp.bottom).offset(6)
            make.size.equalTo(CGSize(width: scaleWidth(80), height: scaleWidth(116)))
        }
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(bookImage.snp.bottom).offset(4)
            make.left.equalTo(4)
            make.right.equalTo(-4)
        }
        bookBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        wn_cornerWithBorder(8)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
